import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:jetour_line/base_widget/base_image.dart';
import 'package:jetour_line/base_widget/base_scaffold.dart';
import 'package:jetour_line/base_widget/loading_widget.dart';
import 'package:jetour_line/models/order_detail_model.dart';
import 'package:jetour_line/my_colors.dart';
import 'package:jetour_line/pages/line/lineDetail/line_detail_page.dart';
import 'package:jetour_line/utils/extension_int.dart';
import 'package:jetour_line/utils/page_push_utils.dart';

import '../../base_widget/my_alert.dart';
import '../../network/aha_http.dart';

class OrderDetailScreen extends StatefulWidget {
  final String id;

  const OrderDetailScreen({Key? key, required this.id}) : super(key: key);

  @override
  State<OrderDetailScreen> createState() => _OrderDetailScreenState();
}

class _OrderDetailScreenState extends State<OrderDetailScreen> {
  OrderDetailModel? detailModel;
  int loadingType = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getDetail();
  }

  @override
  Widget build(BuildContext context) {
    return BaseScaffold(
        backgroundColor: MyColors.colorF1F1F1,
        title: '订单详情',
        body: detailModel == null
            ? LoadingWidget(
                type: loadingType,
              )
            : Stack(
                children: [
                  Container(
                    padding:
                        EdgeInsets.only(left: 20.px, right: 20.px, top: 40.px),
                    width: double.infinity,
                    height: 300.px,
                    color: MyColors.appColor,
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        // BaseImage(image: 'image',width: 30.px,height: 30.px,),
                        Text(
                          detailModel!.statusText,
                          style: TextStyle(
                              fontSize: 40.px,
                              color: MyColors.white,
                              fontWeight: FontWeight.w500),
                        )
                      ],
                    ),
                  ),
                  Padding(
                    padding:
                        EdgeInsets.only(top: 140.px, left: 25.px, right: 25.px),
                    child: ClipRRect(
                        borderRadius: BorderRadius.all(Radius.circular(12.px)),
                        child: Column(
                          children: [
                            Container(
                              width: double.infinity,
                              color: MyColors.white,
                              child: Padding(
                                padding: EdgeInsets.all(30.px),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      detailModel!.caption,
                                      style: TextStyle(
                                          fontSize: 30.px,
                                          color: MyColors.color333333,
                                          fontWeight: FontWeight.w500),
                                    ),
                                    SizedBox(
                                      height: 30.px,
                                    ),
                                    Row(children: [
                                      Text(
                                        '订单金额：',
                                        style: TextStyle(
                                            fontSize: 30.px,
                                            color: MyColors.color999999,),
                                      ),
                                      Text(
                                        '￥${detailModel!.totalPrice}',
                                        style: TextStyle(
                                            fontSize: 28.px,
                                            color: MyColors.colorred),
                                      ),
                                    ],),
                                    Container(
                                      height: 80.px,
                                      child: Row(
                                        mainAxisAlignment: MainAxisAlignment.end,
                                        children: [
                                          const Expanded(child: SizedBox()),
                                          GestureDetector(
                                            onTap: () {
                                              PagePushUtils.push(
                                                  context,
                                                  LineDetailPageScreen(
                                                      id: detailModel!.lineId));
                                            },
                                            child: Container(
                                              //设置 child 居中
                                              alignment: Alignment.center,
                                              height: 50.px,
                                              width: 130.px,
                                              //边框设置
                                              decoration: BoxDecoration(
                                                //背景
                                                color: Colors.white,
                                                //设置四周圆角 角度
                                                borderRadius:
                                                BorderRadius.all(Radius.circular(4.px)),
                                                //设置四周边框
                                                border: Border.all(
                                                    width: 1, color: MyColors.appColor),
                                              ),
                                              child: Text(
                                                "查看路书",
                                                style: TextStyle(
                                                    fontSize: 26.px,
                                                    color: MyColors.appColor),
                                              ),
                                            ),
                                          ),
                                          SizedBox(
                                            width: 24.px,
                                          ),
                                          GestureDetector(
                                            onTap: () {
                                              delOrder();
                                            },
                                            child: Container(
                                              //设置 child 居中
                                              alignment: Alignment.center,
                                              height: 50.px,
                                              width: 130.px,
                                              //边框设置
                                              decoration: BoxDecoration(
                                                //背景
                                                color: Colors.white,
                                                //设置四周圆角 角度
                                                borderRadius:
                                                BorderRadius.all(Radius.circular(4.px)),
                                                //设置四周边框
                                                border: Border.all(
                                                    width: 1, color: MyColors.colorred),
                                              ),
                                              child: Text(
                                                "删除订单",
                                                style: TextStyle(
                                                    fontSize: 26.px,
                                                    color: MyColors.colorred),
                                              ),
                                            ),
                                          ),
                                          SizedBox(
                                            width: 24.px,
                                          )
                                        ],
                                      ),
                                    )

                                  ],
                                ),
                              ),
                            ),
                            SizedBox(
                              height: 30.px,
                            ),
                            ClipRRect(
                              borderRadius:
                                  BorderRadius.all(Radius.circular(12.px)),
                              child: Container(
                                  width: double.infinity,
                                  color: MyColors.white,
                                  child: Padding( padding: EdgeInsets.all(30.px), child: Column(
                                    crossAxisAlignment:
                                    CrossAxisAlignment.start,
                                    children: [
                                      Text(
                                        '订单信息',
                                        style: TextStyle(
                                            fontSize: 30.px,
                                            color: MyColors.color333333,
                                            fontWeight: FontWeight.w500),
                                      ),
                                      SizedBox(
                                        height: 22.px,
                                      ),
                                      Text(
                                        '订单编号：${detailModel!.orderId}',
                                        style: TextStyle(
                                            fontSize: 28.px,
                                            color: MyColors.color666666),
                                      ),
                                      SizedBox(
                                        height: 22.px,
                                      ),
                                      Text(
                                        '订单时间：${detailModel!.createAt}',
                                        style: TextStyle(
                                            fontSize: 28.px,
                                            color: MyColors.color666666),
                                      ),
                                    ],
                                  ),)
                              ),
                            ),
                          ],
                        )),
                  ),

                ],
              ));
  }

  void getDetail() {
    Map<String, dynamic> params = Map();
    params['orderId'] = widget.id;
    AhaHttp().get(AhaRouter.MyOrderDetail, params: params).then((value) {
      detailModel = OrderDetailModel.fromJson(value);
      setState(() {});
    });
  }

  void delOrder() {
    showMyDialog(context, '是否删除该订单', "是", "否", (index) {
      if (index == 0) {
        Map<String, dynamic> params = Map();
        params['orderId'] = widget.id;
        AhaHttp().post(AhaRouter.MyOrderDelete, data: params).then((value) {
          Navigator.pop(context, 'del');
        });
      }
    });
  }
}
